package flc.ast.activity;

import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MessageAdapter;
import flc.ast.bean.MyDebugBean;
import flc.ast.bean.MyMessageBean;
import flc.ast.databinding.ActivityUdpServerBinding;
import flc.ast.dialog.FormatDialog;
import flc.ast.utils.TCPServer;
import flc.ast.utils.UDPServer;

public class UdpServerActivity extends BaseAc<ActivityUdpServerBinding> {

    public static MyDebugBean debugBean;
    private boolean isStart;
    private UDPServer udpServer;
    private MessageAdapter messageAdapter;
    private List<MyMessageBean> listMessage = new ArrayList<>();
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");

    @Override
    protected int onCreate() {
        return R.layout.activity_udp_server;
    }

    @Override
    protected void initView() {
        mDataBinding.ivUdpServerBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvStartListen.setOnClickListener(this);
        mDataBinding.tvContentEncode.setOnClickListener(this);
        mDataBinding.tvContentClear.setOnClickListener(this);
        mDataBinding.tvSendEncode.setOnClickListener(this);
        mDataBinding.tvSendClear.setOnClickListener(this);
        mDataBinding.tvSendMessage.setOnClickListener(this);

        mDataBinding.rvMessageList.setLayoutManager(new LinearLayoutManager(mContext));
        messageAdapter = new MessageAdapter();
        mDataBinding.rvMessageList.setAdapter(messageAdapter);
        messageAdapter.setList(listMessage);

        udpServer = new UDPServer();
        udpServer.start(Integer.parseInt(debugBean.getPort()));

        udpServer.setMessageListener(new UDPServer.MessageListener() {
            @Override
            public void onMessageReceived(String message) {
                if (isStart) {
                    listMessage.add(new MyMessageBean(TimeUtils.getNowString(simpleDateFormat) + "->", message, 1));
                    messageAdapter.setList(listMessage);
                    mDataBinding.rvMessageList.smoothScrollToPosition(listMessage.size() - 1);
                }
            }

            @Override
            public void onClientActivity(String ip) {

            }

            @Override
            public void onStatisticsUpdated(int receivedCount, int sentCount, double receiveSpeed, double sendSpeed, int failedCount) {
                mDataBinding.tvReceive.setText(String.valueOf(receivedCount));
                mDataBinding.tvReceiveSpeed.setText(((int) receiveSpeed) + getString(R.string.byte_speed_unit));
                mDataBinding.tvSend.setText(String.valueOf(sentCount));
                mDataBinding.tvSendSpeed.setText(((int) sendSpeed) + getString(R.string.byte_speed_unit));
                mDataBinding.tvError.setText(String.valueOf(failedCount));
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvStartListen) {//开始/停止监听
            if (isStart) {
                isStart = false;
                mDataBinding.tvStartListen.setText(getText(R.string.start_listen));
                listMessage.add(new MyMessageBean(TimeUtils.getNowString(simpleDateFormat) + "->", getString(R.string.stop_listen), 0));
                messageAdapter.setList(listMessage);
                mDataBinding.rvMessageList.smoothScrollToPosition(listMessage.size() - 1);
            } else {
                isStart = true;
                mDataBinding.tvStartListen.setText(getText(R.string.stop_listen));
                listMessage.add(new MyMessageBean(TimeUtils.getNowString(simpleDateFormat) + "->", getString(R.string.start_listen), 0));
                messageAdapter.setList(listMessage);
                mDataBinding.rvMessageList.smoothScrollToPosition(listMessage.size() - 1);
            }
        } else if (id == R.id.tvContentEncode) {//接收消息编码
            showContentFormatDialog();
        } else if (id == R.id.tvContentClear) {//接收消息清空
            listMessage.clear();
            messageAdapter.setList(listMessage);
        } else if (id == R.id.tvSendEncode) {//发送消息编码
            showSendFormatDialog();
        } else if (id == R.id.tvSendClear) {//发送消息清空
            mDataBinding.etMessage.setText("");
        } else if (id == R.id.tvSendMessage) {//发送消息
            String message = mDataBinding.etMessage.getText().toString();
            if (TextUtils.isEmpty(message)) {
                ToastUtils.showShort(getString(R.string.please_input));
                return;
            }
            udpServer.sendMessageToAllClients(message);
            listMessage.add(new MyMessageBean(TimeUtils.getNowString(simpleDateFormat) + "->", message, 2));
            messageAdapter.setList(listMessage);
            mDataBinding.rvMessageList.smoothScrollToPosition(listMessage.size() - 1);
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvUdpServerTitle.setText(debugBean.getName() + "\n" + debugBean.getHost() + ":" + debugBean.getPort());
        mDataBinding.tvReceive.setText(String.valueOf(0));
        mDataBinding.tvReceiveSpeed.setText(0 + getString(R.string.byte_speed_unit));
        mDataBinding.tvSend.setText(String.valueOf(0));
        mDataBinding.tvSendSpeed.setText(0 + getString(R.string.byte_speed_unit));
        mDataBinding.tvError.setText(String.valueOf(0));
    }

    private void showContentFormatDialog() {
        FormatDialog dialog = new FormatDialog(mContext);
        dialog.setFormat(mDataBinding.tvContentEncode.getText().toString());
        dialog.setListener(new FormatDialog.Ilistener() {
            @Override
            public void clickConfirm(String format) {
                mDataBinding.tvContentEncode.setText(format);
                udpServer.setReceiveCharsetName(format.equals(getString(R.string.encode_text1)) ? getString(R.string.encode_text2) : format);
            }
        });
        dialog.show();
    }

    private void showSendFormatDialog() {
        FormatDialog dialog = new FormatDialog(mContext);
        dialog.setFormat(mDataBinding.tvSendEncode.getText().toString());
        dialog.setListener(new FormatDialog.Ilistener() {
            @Override
            public void clickConfirm(String format) {
                mDataBinding.tvSendEncode.setText(format);
                udpServer.setSendCharsetName(format.equals(getString(R.string.encode_text1)) ? getString(R.string.encode_text2) : format);
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (udpServer != null) {
            udpServer.stop();
        }
    }

}